package com.sch.wanandroid.ui.girls

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sch.wanandroid.entity.GirlEntity
import com.sch.wanandroid.network.NetWorkManager
import kotlinx.coroutines.launch

/**
 * Created by Sch.
 * Date: 2020/12/16
 * description:
 */
class GirlsViewModel : ViewModel() {
    private val _grils = MutableLiveData<List<GirlEntity>>()
    val girlsLiveData: LiveData<List<GirlEntity>> = _grils
    private val _errorMsg = MutableLiveData<String?>()
    val errorMsg: LiveData<String?> = _errorMsg

    fun getGirls(pageNum: Int) {
        viewModelScope.launch {
            try {
                val result = NetWorkManager.apiService.getGirls(pageNum)
                _grils.value = result.data
            } catch (e: Exception) {
                _errorMsg.value = e.toString()
            }
        }
    }

    fun getRandomGirls() {
        viewModelScope.launch {
            try {
                val result = NetWorkManager.apiService.getRandomGirls()
                if (result.status == 100) {
                    _grils.value = result.data
                } else {
                    _errorMsg.value = result.msg
                }
            } catch (e: Exception) {
                _errorMsg.value = e.toString()
            }
        }
    }
}